

//
//  MyJourneyMainViewController.m
//  didihaha
//
//  Created by 赵忠鹏 on 2017/8/3.
//  Copyright © 2017年 zhaozhongpeng. All rights reserved.
//

#import "MyJourneyMainViewController.h"
#import "MyJourneyMainModel.h"
#import "MyJourneyMainViewModel.h"
#import "OpenInvoiceViewController.h"
#import "MyJourneyDetailViewController.h"

@interface MyJourneyMainViewController ()<UITableViewDelegate>

@property (nonatomic, strong)MyJourneyMainViewModel *MyJourneyVM;

@property (nonatomic,strong)UITableView *MyJourneyTB;

@property (nonatomic,weak)NSArray *tempArr;

@end

@implementation MyJourneyMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"我的行程"];
    [self setLeftBackButton];
    [self setRightButton];
    self.view.backgroundColor = [UIColor colorWithRed:240/255.0 green:241/255.0 blue:242/255.0 alpha:1];
    [self.view addSubview:self.MyJourneyTB];
    [self.MyJourneyTB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.MyJourneyVM refreshDataArr:^(NSArray *dataArr) {
        if (dataArr.count > 0) {
            self.tempArr = dataArr;
            [self.MyJourneyTB reloadData];
        }
    }];
}

- (void)setRightButton{
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"开发票" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButton setTitleColor:[UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    rightButton.frame = CGRectMake(0, 0, 60, 30);
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = right;
    
}

- (void)rightButtonClick{
    
    OpenInvoiceViewController *vc = [[OpenInvoiceViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 懒加载

- (MyJourneyMainViewModel *)MyJourneyVM{
    
    if (_MyJourneyVM == nil) {
        _MyJourneyVM = [[MyJourneyMainViewModel alloc] init];
    }
    return _MyJourneyVM;
    
}

- (UITableView *)MyJourneyTB{
    if (_MyJourneyTB == nil) {
        _MyJourneyTB = [[UITableView alloc] init];
        _MyJourneyTB.separatorStyle = UITableViewCellSeparatorStyleNone;
        _MyJourneyTB.delegate = self;
        _MyJourneyTB.dataSource = self.MyJourneyVM;
    }
    
    return _MyJourneyTB;
}


#pragma mark - 代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 45)];
        bgView.backgroundColor = [UIColor colorWithRed:240/255.0 green:241/255.0 blue:242/255.0 alpha:1];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15,20, 200, 20)];
        label.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
        label.text = @"已完成订单";
        label.font = [UIFont systemFontOfSize:13];
        [bgView addSubview:label];
        return bgView;
        
        
    }else{
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 45;
    }else{
        return 0.001;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyJourneyMainModel *model = self.tempArr[indexPath.row];
    MyJourneyDetailViewController *detailVC = [[MyJourneyDetailViewController alloc] init];
    detailVC.dataDic = model.DetailData;
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
